package com.xmotor.test_getAnnotationValue;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author Dang Xianyue
 * @Date 2020-10-05 18:53
 * @Version 1.0
 */
@Component
public class Init {

    private Map<String, Handler> map = new HashMap<>(16);

    @Autowired
    public void setMap(List<Handler> handlers){
        if (handlers != null && handlers.size() > 0){
            for (Handler handler : handlers) {
                TestAnnotation annotation = AnnotationUtils.findAnnotation(handler.getClass(), TestAnnotation.class);
                int[] typeArr = annotation.type();
                String[] codeArr = annotation.code();
                for (int i = 0; i < typeArr.length; i++) {
                    for (int j = 0; j < codeArr.length; j++) {
                        map.put(typeArr[i] + codeArr[j],handler);
                    }
                }

            }
        }

        System.out.println(JSON.toJSONString(map));
    }
}
